#pragma once

#include <iostream>
#include <sys/epoll.h>
#include <memory>
#include "Socket.hpp"
#include "log.hpp"
#include "Epoller.hpp"
#include "nocopy.hpp"
uint32_t EVENT_IN = (EPOLLIN);
uint32_t EVENT_OUT = (EPOLLOUT);

class EpollServer : public nocopy
{
    static const int num = 64;
public:
    EpollServer(uint16_t port)
        : _port(port), 
          _listsock_ptr(new MySocket()),
          _epoller_ptr(new Epoller())
    {
    }
    void Init()
    {
        _listsock_ptr->Socket();
        _listsock_ptr->Bind(_port);
        _listsock_ptr->Listen();
        log(Info,"create listen socket success:%d",_listsock_ptr->Fd());
    }
    void Start()
    {
        _epoller_ptr->EpollerUpdate(EPOLL_CTL_ADD,_listsock_ptr->Fd(),EVENT_IN);
        struct epoll_event revents[num];
        while (true)
        {
            int n = _epoller_ptr->EpollerWait(revents,num);
            if(n > 0)
            {
                //有事件就绪
                //HandlerEvent(revs,n);
            }
            else if(n == 0)
            {
                log(Info,"time out...");
            }
            else
            {
                log(Error,"epoll wait error");
            }
        }
    }
    ~EpollServer()
    {
        _listsock_ptr->Close();
    }

private:
    std::shared_ptr<MySocket> _listsock_ptr;
    std::shared_ptr<Epoller> _epoller_ptr;
    uint16_t _port;
};